package com.cl.web.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.cl.common.api.CommonResult;
import com.cl.common.constant.MessageConstant;
import com.cl.common.vo.LoginVo;
import com.cl.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api("管理员管理 AdminController")
@RestController
@RequestMapping("/admin/")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("用户登录")
    @PostMapping("login")
    public CommonResult login(@RequestBody LoginVo loginVo) {
        return adminService.login(loginVo);
    }

    @ApiOperation("退出登录")
    @PostMapping("logout")
    public CommonResult logout(@RequestBody LoginVo loginVo) {
        StpUtil.logout(loginVo.getUsername(), MessageConstant.DEVICE);
        return CommonResult.success("退出成功", null);
    }

    @ApiOperation("踢人下线")
    @PostMapping("kickOut")
    public CommonResult kickOut(@RequestBody LoginVo loginVo) {
        StpUtil.kickout(loginVo.getUsername());
        return CommonResult.success(MessageConstant.KICK_SOMEONE_OFFLINE, null);
    }

    @ApiOperation("账号封禁或解封")
    @PostMapping("disabled")
    public CommonResult disabled(@RequestBody LoginVo loginVo) {
        if (!StpUtil.isDisable(loginVo.getUsername())) {
            StpUtil.kickout(loginVo.getUsername());
            StpUtil.disable(loginVo.getUsername(), -1);
            return CommonResult.success(MessageConstant.USER_FAIL, StpUtil.isDisable(loginVo.getUsername()));
        }
        StpUtil.untieDisable(loginVo.getUsername());
        return CommonResult.success(null, StpUtil.isDisable(loginVo.getUsername()));
    }

}

